<?php

/**
 *检查邮箱是否有效
 *@date 2011-1-8 / @time 下午02:28:35
 */
function check_email($email) {
	//检查邮箱是否有效
	if (preg_match ( '/^\w[-.\w]*@(\w[-._\w]*\.[a-zA-Z]{2,}.*)$/', $email, $matches )) {
		if (function_exists ( 'checkdnsrr' )) {
			if (checkdnsrr ( $matches [1] . '.', 'MX' ))
				return true;
			if (checkdnsrr ( $matches [1] . '.', 'A' ))
				return true;
		} else {
			$hostName=$matches['1'];
			if (! empty ( $hostName )) {
				$recType = "MX";
				exec("nslookup -type=$recType $hostName", $result );
				foreach ( $result as $line ) {
					if (eregi ( "^$hostName", $line )) {
						return true;
					}
				}
				return false;
			}
			return false;
		}
	}
	return false;
} //end check_email


/**
 *显示标题
 *@date 2010-12-30 / @time 下午05:51:10
 */
function showtitle($id, $types, $field = 'title') {
	//显示标题
	$title = '';
	switch (true) {
		case $types == 'arc' :
			
			break;
		
		default :
			;
			break;
	}
	return $title;
} //end showtitle


/**
 *显示公共列表名称
 *@date 2010-12-30 / @time 下午05:41:10
 */
function commlist_title($id) {
	//显示公共列表名称
	$arr = S ( "Commlist" );
	if (! $arr) {
		$dao = D ( "Commlist" );
		$arr = $dao->where ( "status=1" )->field ( "id,title,etitle" )->findAll ();
		$arr = getkey ( $arr, 'id' );
		S ( "Commlist", $arr );
	}
	$v = cookie ( 'think_language' ) == 'en-us' ? 'etitle' : 'title';
	return $arr [$id] [$v];
} //end commlist_title


/**
 *获取封面图片
 *@date 2010-12-9 / @time 下午02:40:34
 */
function cover($aid, $album = '0') {
	//获取封面图片
	$dao = D ( "Pic" );
	$condition = array ();
	if ($album == '0') {
		$condition ['aid'] = $aid;
	} else {
		$condition ['album_id'] = $aid;
	}
	$condition ['cover'] = 1;
	return $dao->where ( $condition )->find ();
} //end cover


/**
 *根据数组返回图片路径
 *@date 2010-12-9 / @time 下午03:03:24
 */
function arr2path($arr, $imgarr = '0') {
	//根据数组返回图片路径
	if (empty ( $arr )) {
		return false;
	}
	if ($imgarr == '0') {
		return empty ( $arr ['filename'] ) ? '' : $arr ['filepath'] . $arr ['filename'];
	} else {
		$img = array ();
		foreach ( $arr as $p ) {
			$img [] = $arr ['filepath'] . $arr ['filename'];
		}
		return $img;
	}
} //end arr2path


/**
 *获取服务名称
 *@date 2010-12-7 / @time 下午05:25:27
 */
function server($str) {
	//获取服务名称
	$sname = array ('1' => '', 2 => '', 3 => '', 4 => '' );
	$return = '';
	if (strpos ( $str, ',' )) {
		$str = explode ( ',', $str );
		foreach ( $str as $v ) {
			$return .= $sname [$v] . '|';
		}
		$return = trim ( $return, '|' );
	} else {
		$return = $sname [$str];
	}
	return $return;
} //end server


/**
 *编码需要打开的网址
 *@date 2010-9-8
 *@time 下午06:06:25
 *@param $url 网址
 *@param $id arc表ID
 */
function jump($url, $id=0) {
	//编码需要打开的网址
	$url = $url . '||' . $id;
	$url = myencode ( $url );
	return __APP__ . '/Show/jump/url/' . $url;
} //end function_name


/**
 *自动生成序列号
 *@date 2010-8-26
 *@time 上午10:33:23
 *@param $id 参数ID
 */
function xnum($id = '') {
	//自动生成序列号
	$id = empty ( $id ) ? time () : $id;
	return strtoupper ( substr ( md5 ( substr ( md5 ( $id ), 10, 8 ) ), 10, 8 ) );
} //end xnum


/**
 *根据时间自动生成密码
 *@date 2010-8-26
 *@time 上午10:34:29
 */
function ptime() {
	//根据时间自动生成密码
	return md5 ( time () );
} //end ptime


function br2nl($text) {
	return preg_replace ( '/<br\\s*?\/??>/i', '', $text );
}

function arrayischk($arr, $value = '-100') {
	$re = '';
	if ($value == '-100') {
		$re = empty ( $arr ) ? '' : 'checked="checked"';
	} else {
		$re = ($arr == $value) ? 'checked="checked"' : '';
	}
	return $re;
}

/**
 * 将数组用指定字符连接
 * @param  $array 数组
 * @param  $split 分隔符
 * @return  string
 */
function array2string($array, $split = ',') {
	if (is_array ( $array )) {
		$dot = '';
		foreach ( $array as $data ) {
			$string .= $dot . $data;
			$dot = $split;
		}
		return empty ( $string ) ? '' : $string;
	} else {
		return $array;
	}
}

/**
 * 将指定数据转换成数组再对比，符合条件则返回checked
 *
 * @param  $sring 输入字符
 * @param  $param 条件
 * @param  $split 分隔符
 * @return  string
 */
function string2checked($sring, $param, $split = ',', $type = 'checked') {
	$splitParam = explode ( $split, $param );
	if (in_array ( $sring, $splitParam ))
		$result = " $type=$type";
	return $result;
}

/**
 *
 * 下拉框，单选按钮 自动选择
 * @param  $string 输入字符
 * @param  $param 条件
 * @param  $type 类型  selected checked
 * @return   string
 */
function selected($string, $param = 1, $type = 'select') {
	if ($string == $param) {
		$returnString = $type == 'select' ? 'selected="selected"' : 'checked="checked"';
	}
	return $returnString;
}

/**
 *将/风格的日期转为格林时间
 *@date 2010-6-12
 *@time 下午12:02:45
 */
function ftime($oldtime) {
	//将/风格的日期转为格林时间
	$t = explode ( '/', $oldtime );
	return mktime ( '0', 0, 0, $t ['1'], $t ['0'], $t ['2'] );
} //end ftime


/**
 *转化数组的索引
 *@date 2010-6-7
 *@time 下午09:42:30
 */
function getkey($arr, $k) {
	$narr = array ();
	foreach ( $arr as $v ) {
		$narr [$v [$k]] = $v;
	}
	return $narr;
}

/**
 *格式时间
 *@date 2010-5-20
 *@time 下午08:35:01
 */
function toDate($time, $format = 'Y-m-d H:i:s') {
	if (empty ( $time )) {
		return '';
	}
	$format = str_replace ( '#', ':', $format );
	return date ( auto_charset ( $format ), $time );
}

//返回格林威治标准时间
function MyDate($format = 'Y-m-d H:i:s', $timest = 0) {
	$cfg_cli_time = C ( 'CLI_TIME' );
	$addtime = $cfg_cli_time * 3600;
	if (empty ( $format )) {
		$format = 'Y-m-d H:i:s';
	}
	return gmdate ( $format, $timest + $addtime );
}

function GetMkTime($dtime) {
	$cfg_cli_time = C ( 'CLI_TIME' );
	if (! ereg ( "[^0-9]", $dtime )) {
		return $dtime;
	}
	$dtime = trim ( $dtime );
	$dt = Array (1970, 1, 1, 0, 0, 0 );
	$dtime = ereg_replace ( "[\r\n\t]|日|秒", " ", $dtime );
	$dtime = str_replace ( "年", "-", $dtime );
	$dtime = str_replace ( "月", "-", $dtime );
	$dtime = str_replace ( "时", ":", $dtime );
	$dtime = str_replace ( "分", ":", $dtime );
	$dtime = trim ( ereg_replace ( "[ ]{1,}", " ", $dtime ) );
	$ds = explode ( " ", $dtime );
	$ymd = explode ( "-", $ds [0] );
	if (! isset ( $ymd [1] )) {
		$ymd = explode ( ".", $ds [0] );
	}
	if (isset ( $ymd [0] )) {
		$dt [0] = $ymd [0];
	}
	if (isset ( $ymd [1] )) {
		$dt [1] = $ymd [1];
	}
	if (isset ( $ymd [2] )) {
		$dt [2] = $ymd [2];
	}
	if (strlen ( $dt [0] ) == 2) {
		$dt [0] = '20' . $dt [0];
	}
	if (isset ( $ds [1] )) {
		$hms = explode ( ":", $ds [1] );
		if (isset ( $hms [0] )) {
			$dt [3] = $hms [0];
		}
		if (isset ( $hms [1] )) {
			$dt [4] = $hms [1];
		}
		if (isset ( $hms [2] )) {
			$dt [5] = $hms [2];
		}
	}
	foreach ( $dt as $k => $v ) {
		$v = ereg_replace ( "^0{1,}", '', trim ( $v ) );
		if ($v == '') {
			$dt [$k] = 0;
		}
	}
	$mt = @gmmktime ( $dt [3], $dt [4], $dt [5], $dt [1], $dt [2], $dt [0] ) - 3600 * $cfg_cli_time;
	if (! empty ( $mt )) {
		return $mt;
	} else {
		return time ();
	}
}

function SubDay($ntime, $ctime) {
	$dayst = 3600 * 24;
	$cday = ceil ( ($ntime - $ctime) / $dayst );
	return $cday;
}

function AddDay($ntime, $aday) {
	$dayst = 3600 * 24;
	$oktime = $ntime + ($aday * $dayst);
	return $oktime;
}

function GetDateTimeMk($mktime) {
	return MyDate ( 'Y-m-d H:i:s', $mktime );
}

function GetDateMk($mktime) {
	return MyDate ( "Y-m-d", $mktime );
}

function ru($url, $p = array(), $h = true, $r = false) {
	return U ( $url, $p, $r, $h );
}
function IP($ip = '', $file = 'UTFWry.dat') {
	$ip = $ip == "0" ? "unknown" : long2ip ( $ip );
	$_ip = array ();
	if (isset ( $_ip [$ip] )) {
		return $_ip [$ip];
	} else {
		import ( "ORG.Net.IpLocation" );
		$iplocation = new IpLocation ( $file );
		$location = $iplocation->getlocation ( $ip );
		$_ip [$ip] = $location ['country'] . $location ['area'];
	}
	return $_ip [$ip];
}

// 获取客户端IP地址
function client_ip() {
	if (getenv ( "HTTP_CLIENT_IP" ) && strcasecmp ( getenv ( "HTTP_CLIENT_IP" ), "unknown" ))
		$ip = getenv ( "HTTP_CLIENT_IP" );
	else if (getenv ( "HTTP_X_FORWARDED_FOR" ) && strcasecmp ( getenv ( "HTTP_X_FORWARDED_FOR" ), "unknown" ))
		$ip = getenv ( "HTTP_X_FORWARDED_FOR" );
	else if (getenv ( "REMOTE_ADDR" ) && strcasecmp ( getenv ( "REMOTE_ADDR" ), "unknown" ))
		$ip = getenv ( "REMOTE_ADDR" );
	else if (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], "unknown" ))
		$ip = $_SERVER ['REMOTE_ADDR'];
	else
		$ip = "0";
	$ip = $ip == "0" ? "0" : ip2long ( $ip );
	return ($ip);
}

function myencode($string) {
	$data = base64_encode ( $string );
	$data = str_replace ( array ('+', '/', '=' ), array ('-', '_', '' ), $data );
	return $data;
}

function mydecode($string) {
	$data = str_replace ( array ('-', '_' ), array ('+', '/' ), $string );
	$mod4 = strlen ( $data ) % 4;
	if ($mod4) {
		$data .= substr ( '====', $mod4 );
	}
	return base64_decode ( $data );
}

/**
 * 返回格式化文件尺寸
 * 
 * @param Int $size 文件尺寸单位（B）
 */
function RealSize($size) {
	if ($size < 1024) {
		return $size . ' Byte';
	}
	if ($size < 1048576) {
		return round ( $size / 1024, 2 ) . ' KB';
	}
	if ($size < 1073741824) {
		return round ( $size / 1048576, 2 ) . ' MB';
	}
	if ($size < 1099511627776) {
		return round ( $size / 1073741824, 2 ) . ' GB';
	}
}
?>